package com.hotelsystem.service.impl;

import com.hotelsystem.dao.RoomTypeMapper;
import com.hotelsystem.pojo.RoomType;
import com.hotelsystem.service.RoomTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class RoomTypeServiceImpl implements RoomTypeService {

    @Resource(name = "roomTypeMapper")
    private RoomTypeMapper roomTypeMapper;
//    @Resource
//    private RedisTemplate<Object,Object> redisTemplate;

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<RoomType> listByTypeNameAndStatus(String typeName, String[] status) throws Exception {
        return roomTypeMapper.listByTypeNameAndStatus(typeName,status);
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<RoomType> getAllRoomType() throws Exception {

//        Object getAllRoomType = redisTemplate.opsForValue().get("getAllRoomTypeLists");
//        if (getAllRoomType==null){
            List<RoomType> getAllRoomType = roomTypeMapper.getAllRoomType();
//            redisTemplate.opsForValue().set("getAllRoomTypeLists",getAllRoomType);
//        }
        return (List<RoomType>) getAllRoomType;
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<RoomType> getRoomTypesByRoomType(RoomType roomType) throws Exception {
        return roomTypeMapper.getRoomTypesByRoomType(roomType);
    }

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public RoomType getRoomTypeInfoById(Integer roomTypeId) throws Exception {
        return roomTypeMapper.getRoomTypeInfoById(roomTypeId);
    }

    @Override
    public Integer updateRoomTypeByRoomType(RoomType roomType) throws Exception {


        //删除缓存
//        redisTemplate.delete("getAllRoomTypeLists");
        //修改信息
        return roomTypeMapper.updateRoomTypeByRoomType(roomType);
    }

    @Override
    public Integer deleteRoomTypeBytypeId(Integer roomTypeId) throws Exception {
        //先通过typeId查询出信息
        RoomType roomType = getRoomTypeInfoById(roomTypeId);
        Integer num = roomType.getReserved()+roomType.getCheckIn();
        if (num>0){
            return 0;
        }else {
            //删除缓存
//            redisTemplate.delete("getAllRoomTypeLists");
            roomTypeMapper.deleteRoomTypeBytypeId(roomTypeId);
            return 1;
        }
    }
    @Override
    public Integer addRoomTypeByRoomType(RoomType roomType) throws Exception {
        //可用的和房间数是一致的
        roomType.setUsable(0);
        roomType.setReserved(0);
        roomType.setCheckIn(0);
        //根据房间数 来判断其状态
        roomType.setRoomNum(0);
        //修改类型状态
        roomType.setStatus(2);
        //删除缓存
//        redisTemplate.delete("getAllRoomTypeLists");
        return roomTypeMapper.addRoomTypeByRoomType(roomType);
    }
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public RoomType getRoomTypeBytypeName(String typeName) throws Exception {
        return roomTypeMapper.getRoomTypeBytypeName(typeName);
    }

    /**
     * 通过类型查询出当前房屋可入住可预定的和 然后推出当前可用的房间数
     * @param roomTypeId
     * @return
     * @throws Exception
     */
    @Override
    public Integer getAbleCheckInAndReserveSum(Integer roomTypeId) throws Exception {
        return roomTypeMapper.getAbleCheckInAndReserveSum(roomTypeId);
    }

}
